//
//  LGTrainRecordViewController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/8/10.
//  运动训练记录

#import "LGSportsTrainRecordController.h"
#import "LGSportsTrainRecordCell.h"
//自我评估
#import "LGSportsTrainMineEvaView.h"
#import "LGPickerViewMax.h"
@interface LGSportsTrainRecordController ()<UITableViewDataSource,UITableViewDelegate>
/**   */
@property (nonatomic , strong)UIButton *yearButton;
/**   */
@property (nonatomic , strong)UIButton *monthButton;
/**   */
@property (nonatomic , strong)UITableView *tableView;
/**   */
@property (nonatomic , strong)UILabel *countLabel;
/**   */
@property (nonatomic , copy)NSString *year;
/**   */
@property (nonatomic , copy)NSString *month;
/**   */
@property (nonatomic , strong)NSMutableArray *yearArray;
/**   */
@property (nonatomic , strong)NSMutableArray *monthArray;
/**   */
@property (nonatomic , strong)NSMutableArray *dataArray;
/**   */
@property (nonatomic , copy)NSString *pageNo;
/**   */
@property (nonatomic , strong)NSDateFormatter *formatter;
@end

@implementation LGSportsTrainRecordController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.year = @"全部年份";
    self.month = @"全部月份";
    self.title = @"运动训练记录";
    self.view.backgroundColor = [UIColor colorWithString:@"#FFFFFF"];
    NSInteger currentYear = [[[NSCalendar currentCalendar] components:NSCalendarUnitYear fromDate:[NSDate date]] year];
    for (NSInteger i=2000; i<=currentYear; i++) {
        [self.yearArray addObject:[NSString stringWithFormat:@"%ld",i]];
    }
    [self.yearArray addObject:@"全部年份"];
    for (NSInteger i=1; i<13; i++) {
        [self.monthArray addObject:[NSString stringWithFormat:@"%.2ld",i]];
    }
    [self.monthArray addObject:@"全部月份"];
    [self.view addSubview:self.yearButton];
    [self.view addSubview:self.monthButton];    
    [self.view addSubview:self.tableView];
    [self emptyViewWithSuperView:self.view frame:self.tableView.frame];
    [self requestData];
}

//模块被选中
-(void)viewWillAppearAction{
    [self requestData];
}

-(void)requestData{
    self.pageNo = @"1";
    self.dataArray = [NSMutableArray array];
    [self requestMoreData];
}

-(void)requestMoreData{
    NSString *year = [self.year isEqualToString:@"全部年份"]?@"":self.year;
    NSString *month = [self.month isEqualToString:@"全部月份"]?@"":self.month;
    [RequestUtil withPOST:@"/doctor/sport.patient_record/pagedList" parameters:@{@"patient_id":self.patientid,@"pageNo":self.pageNo,@"pageSize":@"20",@"year":year,@"month":month} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        self.isfirst = NO;
        [self.emptyView stopBufferAction];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        if(code == 200){
            self.countLabel.text = [NSString stringWithFormat:@"已完成 %@ 次",returnData[@"total"]];
            NSArray *tempArray = returnData[@"data"];
            if(tempArray.count>0){
                self.pageNo = [NSString stringWithFormat:@"%ld",[returnData[@"current_page"] integerValue]+1];
                [self.dataArray addObjectsFromArray:tempArray];
            }else{
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }
        }
        [self.tableView reloadData];
        if(self.dataArray.count == 0){
            [self.emptyView showViewWithImage:@"emptyView2" content:@"暂无训练记录" offset:0];
        }
    } error:^(NSError *error) {
        self.isfirst = NO;
        [self.emptyView stopBufferAction];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        if(self.dataArray.count == 0){
            [self.emptyView showViewWithImage:@"emptyView2" content:@"暂无训练记录" offset:0];
        }
    }];
}

-(void)selectButtonAction:(UIButton *)sender{
    LGPickerViewMax *pickerView = [[LGPickerViewMax alloc]initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H)];
    pickerView.pickArry = (sender==self.yearButton)?@[self.yearArray]:@[self.monthArray];
    if(sender == self.yearButton){
        pickerView.selectStr = (self.year.length == 0)?@"全部年份":self.year;
    }else{
        pickerView.selectStr = (self.month.length == 0)?@"全部月份":self.month;
    }
    [self.view.window addSubview:pickerView];
    __weak typeof(self) weakSelf = self;
    pickerView.selectPickViewResult = ^(NSArray *resultArray) {
        if(sender == weakSelf.yearButton){
            weakSelf.year = resultArray[0];
        }else{
            weakSelf.month = resultArray[0];
        }
        [weakSelf requestData];
    };
}

-(void)setYear:(NSString *)year{
    _year = year;
    if([year isEqualToString:@"全部年份"]){
        [self.yearButton setTitle:year forState:UIControlStateNormal];
    }else{
        NSString *content = [NSString stringWithFormat:@"%@年",year];
        [self.yearButton setTitle:content forState:UIControlStateNormal];
    }
    [self.yearButton setButtonInsetsStyle:LGButtonInsetsStyleRight marginSpace:8];
}

-(void)setMonth:(NSString *)month{
    _month = month;
    if([month isEqualToString:@"全部月份"]){
        [self.monthButton setTitle:month forState:UIControlStateNormal];
    }else{
        NSString *content = [NSString stringWithFormat:@"%@月",month];
        [self.monthButton setTitle:content forState:UIControlStateNormal];
    }
    [self.monthButton setButtonInsetsStyle:LGButtonInsetsStyleRight marginSpace:8];
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LGSportsTrainRecordCell *textCell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    textCell.selectionStyle = UITableViewCellSelectionStyleNone;
    textCell.dataDic = self.dataArray[indexPath.row];
    return textCell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *tempDic = self.dataArray[indexPath.row];
    [RequestUtil withPOST:@"/doctor/sport.patient_record/detail" parameters:@{@"record_id":LGNSString(tempDic[@"record_id"])} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        if(code == 200){
            LGSportsTrainMineEvaView *evaView = [[LGSportsTrainMineEvaView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H)];
            evaView.dataArray = returnData[@"question"];
            [[UIApplication sharedApplication].keyWindow addSubview:evaView];
            
        }else{
            [TooltipView showMessage:message offset:0];
        }
    } error:^(NSError *error) {
        [TooltipView showMessage:@"暂无自我评估" offset:0];
    }];
}


-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, 0.01)];
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, 0.01)];
}


#pragma mark
#pragma mark ====> 创建控件
-(NSMutableArray *)yearArray{
    if(!_yearArray){
        _yearArray = [NSMutableArray array];
    }
    return _yearArray;
}

-(NSMutableArray *)monthArray{
    if(!_monthArray){
        _monthArray = [NSMutableArray array];
    }
    return _monthArray;
}
-(UIButton *)yearButton{
    if(!_yearButton){
        _yearButton = [UIButton buttonWithTitle:@"全部年份" titleFont:LGFont(16) textColor:@"#333333" imageName:@"grayTipDown2"];
        _yearButton.frame = CGRectMake((Screen_W-viewPix(280))/2.0-iPadMargin(50), viewPix(6), viewPix(140), viewPix(42));
        
        [_yearButton addTarget:self action:@selector(selectButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [_yearButton setButtonInsetsStyle:LGButtonInsetsStyleRight marginSpace:8];
    }
    return _yearButton;
}

-(UIButton *)monthButton{
    if(!_monthButton){
        _monthButton = [UIButton buttonWithTitle:@"全部月份" titleFont:LGFont(16) textColor:@"#333333" imageName:@"grayTipDown2"];
        _monthButton.frame = CGRectMake(kMaxX(self.yearButton.frame)+iPadMargin(100), viewPix(6), viewPix(140), viewPix(42));
        [_monthButton addTarget:self action:@selector(selectButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [_monthButton setButtonInsetsStyle:LGButtonInsetsStyleRight marginSpace:8];
    }
    return _monthButton;
}



-(UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, viewPix(50), Screen_W, Screen_H-topBarHeight-viewPix(50)) style:UITableViewStyleGrouped];
        [_tableView registerClass:[LGSportsTrainRecordCell class] forCellReuseIdentifier:@"cell"];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.estimatedRowHeight = viewPix(190);
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.tableHeaderView = [self tableHeaderView];
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(requestData)];
        _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(requestMoreData)];
    }
    return _tableView;
}

-(UILabel *)countLabel{
    if(!_countLabel){
        _countLabel = [UILabel lableWithText:@"" colorString:@"#000000" textFont:LGFont(16) textAlignment:NSTextAlignmentLeft lines:1];
        _countLabel.frame = CGRectMake(viewPix(18), viewPix(16), viewPix(150), viewPix(22));
    }
    return _countLabel;
}

-(UIView *)tableHeaderView{
    UIView *baseView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(42))];
    baseView.backgroundColor = [UIColor colorWithString:@"#F9F9F9"];
    [baseView addSubview:self.countLabel];
    return baseView;
}

@end
